import { defineStore } from "pinia"
import $axios from '../utils/axios'
import { ElNotification } from 'element-plus'

export const useUserStore = defineStore('user', {
    state: () => {
        return {
            roleId: -1,
            searchName: '',
            formData: {},
            userData: [],
            currentPage: 1,
            pageSize: 5,
            total: '',
            dialogVisible: false,
        }
    },
    actions: {
        async getUsers() {
            const path = '/user/selectByRidByPage/' + 4 + '/' + this.currentPage + '/' + this.pageSize
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.userData = res.result
                this.total = parseInt(res.msg)
            }
        },
        async selectByNameOrIdNum() {
            if (this.searchName === '' && this.roleId === 4) {
                this.getUsers()
            } else if (this.searchName === '' && this.roleId === 2) {
                this.getAllWorkers()
            } else if (this.searchName === '' && this.roleId === 3) {
                this.getAllDoctors()
            } else {
                const path = '/user/selectByNameOrIdNum/' + this.searchName
                const { data: res } = await $axios.get(path)
                if (res.code === 200) {
                    this.userData = res.result
                    console.log(this.userData.length);
                    this.total = this.userData.length
                }
            }
        },
        async getAllUser() {
            const path = '/user/selectByRid/' + 4
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.userData = res.result
            }
        },
        async getAllDoctors() {
            const path = '/user/selectByRid/' + 3
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.userData = res.result
            }
        },
        async getAllWorkers() {
            const path = '/user/selectByRid/' + 2
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.userData = res.result
            }
        },
        pre4Add() {
            this.dialogVisible = true
        },
        async addAllUser() {
            const path = '/user/insert'
            const { data: res } = await $axios.put(path, this.formData)
            if (res.code === 200) {
                this.dialogVisible = false
                // tableData.value.push(form.value)
                ElNotification({
                    title: '成功',
                    message: '已成功添加!',
                    type: 'success',
                })
            } else if (res.code === 104) {
                ElNotification({
                    title: '失败',
                    message: '用户名已存在，添加失败！',
                    type: 'error',
                })
            } else if (res.code === 105) {
                ElNotification({
                    title: '失败',
                    message: '手机号已存在，添加失败！',
                    type: 'error',
                })
            } else {
                ElNotification({
                    title: '失败',
                    message: '未知原因，添加失败！',
                    type: 'error',
                })
            }
        },
        async logout(index, row) {
            row.roleId = 0
            this.formData = JSON.parse(JSON.stringify(row))
            const path = '/user/update'
            const { data: res } = await $axios.post(path, this.formData)
            if (res.code === 200) {
                ElNotification({
                    title: '成功',
                    message: '已将' + row.name + '账号注销',
                    type: 'success',
                })
            } else if (res.code === 103) {
                ElNotification({
                    title: '失败',
                    message: '修改失败',
                    type: 'error',
                })
            }
        },
    },
})